package com.itheima.controller;


import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    //ccaabb
    @Autowired
    private ClazzService clazzService;

    @RequestMapping
    public Result list(){

        List<Clazz> clazzLisst = clazzService.findAll();

        return Result.success(clazzLisst);
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        System.out.println("新增班级，clazz = " + clazz);
        return Result.success();
    }

    @GetMapping
    public Result pageQuery(ClazzQueryParam clazzQueryParam){
        PageResult pageResult = clazzService.pageQuery(clazzQueryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询班级信息：{}",id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除班级信息，{}",id);
        clazzService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAll(){
        log.info("查询全部班级信息");
        List<Clazz> clazzListAll = clazzService.clazzListAll();
        return Result.success(clazzListAll);
    }

}
